/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sca.interfaces;

import br.com.sca.dao.CarterinhaDAO;
import br.com.sca.entidades.Carterinha;
import br.com.sca.util.AcessoBD;
import java.awt.Frame;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Leonir
 */
public class CarterinhasVencidasFrame extends javax.swing.JInternalFrame {

    private DefaultTableModel tableModel;
    private ListSelectionModel listModel;
    private List<Carterinha> carterinhas;
    private Carterinha carterinha;

    /**
     * Creates new form CarterinhasVencidasFrame
     */
    public CarterinhasVencidasFrame() {
        initComponents();
        defineModelo();
        atualizarTabela();
    }

    private void defineModelo() {

        tableModel = (DefaultTableModel) tblCateirinhaVencida.getModel();

        listModel = tblCateirinhaVencida.getSelectionModel();
        listModel.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                mostraDetalheCarterinhaVencidas();
            }
        });
    }

    private void atualizarTabela() {
        CarterinhaDAO dao = new CarterinhaDAO();
        try {
            carterinhas = dao.ListarCarterinhasVencidas();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "Erro ao Carregar lista de Carerinhas Vencidas", "Erro", JOptionPane.ERROR_MESSAGE);
        }

        int numLinha = tableModel.getRowCount();
        for (int i = 0; i < numLinha; i++) {
            tableModel.removeRow(0);
        }

        for (int i = 0; i < carterinhas.size(); i++) {
            String dataEmissao = new SimpleDateFormat("dd/MM/yyyy").format(carterinhas.get(i).getDataEmissao());
            String dataValidade = new SimpleDateFormat("dd/MM/yyyy").format(carterinhas.get(i).getDataValidade());
            tableModel.insertRow(i, new Object[]{carterinhas.get(i).getId(), carterinhas.get(i).getAluno().getNome(), dataEmissao, dataValidade});
        }
    }

    private void mostraDetalheCarterinhaVencidas() {
        // mostrar detalhes
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblCateirinhaVencida = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Lista de Carteirinhas Vencidas");
        setAutoscrolls(true);
        setFocusable(false);
        setInheritsPopupMenu(true);
        setPreferredSize(new java.awt.Dimension(600, 450));
        setRequestFocusEnabled(false);

        jPanel4.setBackground(new java.awt.Color(0, 51, 255));

        jLabel2.setFont(new java.awt.Font("Times New Roman", 1, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Lista de Carteirinha Vencidas");
        jLabel2.setFocusable(false);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(211, 211, 211)
                .addComponent(jLabel2)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addContainerGap())
        );

        tblCateirinhaVencida.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nome", "DT. Emissão", "DT. Validade"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblCateirinhaVencida);
        if (tblCateirinhaVencida.getColumnModel().getColumnCount() > 0) {
            tblCateirinhaVencida.getColumnModel().getColumn(0).setMinWidth(50);
            tblCateirinhaVencida.getColumnModel().getColumn(0).setMaxWidth(50);
            tblCateirinhaVencida.getColumnModel().getColumn(1).setMinWidth(350);
            tblCateirinhaVencida.getColumnModel().getColumn(1).setMaxWidth(350);
            tblCateirinhaVencida.getColumnModel().getColumn(2).setMinWidth(100);
            tblCateirinhaVencida.getColumnModel().getColumn(2).setMaxWidth(100);
            tblCateirinhaVencida.getColumnModel().getColumn(3).setMinWidth(100);
            tblCateirinhaVencida.getColumnModel().getColumn(3).setMaxWidth(100);
        }

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
        );

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jButton1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jButton1.setText("Imprimir");
        jButton1.setMargin(new java.awt.Insets(5, 5, 5, 5));
        jButton1.setMaximumSize(new java.awt.Dimension(100, 23));
        jButton1.setMinimumSize(new java.awt.Dimension(85, 23));
        jButton1.setPreferredSize(new java.awt.Dimension(100, 30));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 0, -1, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 600, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        AcessoBD conexao = new AcessoBD();
        try {
            HashMap parametros = new HashMap();
            parametros.put("DATA_ATUAL", new Date());
            parametros.put("IMAGEM", System.getProperty("user.dir") + "\\img\\logo2.jpg");
            JasperPrint jp = JasperFillManager.fillReport(System.getProperty("user.dir") + "\\relatorios\\CarteirinhasVencidas.jasper", parametros, conexao.getConexao());
            JasperViewer jv = new JasperViewer(jp, false);
            jv.setExtendedState(Frame.MAXIMIZED_BOTH);
            jv.setVisible(true);
        } catch (JRException ex) {
            System.out.println("JRException " + ex.getMessage());
            Logger.getLogger(CarterinhasVencidasFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            System.out.println("SQLException " + ex.getMessage());
            Logger.getLogger(CarterinhasVencidasFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblCateirinhaVencida;
    // End of variables declaration//GEN-END:variables
}
